<%@page import="classes.AccountManager"%>
<%@page import="classes.GroupManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="classes.SubjectManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="classes.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>
	<%
		out.println(request.getParameter("groupName"));
	%>
</title>
</head>
<body>
	<%
		GroupManager gmanager = (GroupManager) getServletContext()
				.getAttribute(GroupManager.GROUPMANAGER);
		AccountManager amanager = (AccountManager) getServletContext()
				.getAttribute(AccountManager.ACCOUNTMANAGER);
		SubjectManager smanager = (SubjectManager) getServletContext()
				.getAttribute(SubjectManager.SUBJECTMANAGER);
		int subject_id = smanager.getSubjectId(request
				.getParameter("subject"));
		int group_id = gmanager.getGroupId(
				request.getParameter("groupName"), subject_id);
		if (request.getParameter("bool") != null) {
			if (request.getParameter("students") != null) {
				String nameAndSurname = request.getParameter("students");
				String name = nameAndSurname.substring(0,
						nameAndSurname.indexOf(" "));
				String surname = nameAndSurname.substring(
						nameAndSurname.indexOf(" ") + 1,
						nameAndSurname.length());
				int student_id = amanager.getUserId(name, surname);
				gmanager.addStudentToGroup(student_id, group_id);
			}
		}
		
		out.println("<form action=\"groupStudents.jsp\">");
		out.println("<h1>All Students</h1>");
		out.println("<SELECT NAME=\"students\" SIZE=\"15\" >");
		User[] users = amanager.getUsersByStatus("3");
		if (users != null) {
			for (int i = 0; i < users.length; i++) {
				out.println("<OPTION> " + users[i].getName() + " "
						+ users[i].getSurName());
			}
		}
		out.println("</SELECT>");
	%>
	<INPUT type="hidden" value="<%=request.getParameter("groupName")%>"
		name="groupName">
	<INPUT type="hidden" value="<%=request.getParameter("bool")%>"
		name="bool">
	<INPUT type="hidden" value="<%=request.getParameter("subject")%>"
		name="subject">
	<input type=submit name=add value=Add>
	</form>
	<style type="text/css">
select {
	width: 150px;
	text-align: left;
}

select .lt {
	text-align: left;
}
</style>

	<%
		if (request.getParameter("bool2") != null) {
			if (request.getParameter("students") != null) {
				String nameAndSurname = request.getParameter("students");
				String name = nameAndSurname.substring(0,
						nameAndSurname.indexOf(" "));
				String surname = nameAndSurname.substring(
						nameAndSurname.indexOf(" ") + 1,
						nameAndSurname.length());
				int student_id = amanager.getUserId(name, surname);
				gmanager.deleteStudentFromGroup(student_id, group_id);
			}
		}
		out.println("<form action=\"groupStudents.jsp\">");
		out.println("<h1>Students of " + request.getParameter("groupName")
				+ "</h1>");
		out.println("<SELECT NAME=\"students\" SIZE=\"15\" class=\"ddList\">");
		ArrayList<Integer> arr = GroupManager.getStundetsOfGroup(group_id);
		for (int i = 0; i < arr.size(); i++) {
			User user = amanager.getUser(AccountManager.getUsername(arr.get(i)));
			out.println("<OPTION class=\"lt\">" + user.getName() + " "
					+ user.getSurName() + "</OPTION>");
		}
		out.println("</SELECT>");
	%>
	<INPUT type="hidden" value="<%=request.getParameter("groupName")%>"
		name="groupName">
	<INPUT type="hidden" value="<%=request.getParameter("bool2")%>"
		name="bool2">
	<INPUT type="hidden" value="<%=request.getParameter("subject")%>"
		name="subject">
	<input type=submit value=Remove>
	</form>
	<%
		HttpSession ses = request.getSession();
		User user = (User) ses.getAttribute("user");
	%>
	<form action="groupsForLectures.jsp" method="post">
		<input type="submit" name=butt value="Submit"> <INPUT
			type="hidden" value="<%=request.getParameter("subject")%>"
			name="subject"> <INPUT type="hidden"
			value="<%=user.getUserId()%>" name="id">
	</form>
	<form action="groupsForLectures.jsp" method="post">
		<input type="submit" name=butt value="Delete This Group"> <INPUT
			type="hidden" value="<%=request.getParameter("subject")%>"
			name="subject"> <INPUT type="hidden"
			value="<%=user.getUserId()%>" name="id"> <INPUT type="hidden"
			value="<%=""%>" name="delete"> <INPUT type="hidden"
			value="<%=group_id%>" name="group_id">
	</form>

	<form action="groupsForLectures.jsp" method="post">
		New Group Name: <input type="text" name=newGroupName value="">
		<input type="submit" name=butt value="Rename"> <INPUT
			type="hidden" value="<%=request.getParameter("subject")%>"
			name="subject"> <INPUT type="hidden"
			value="<%=user.getUserId()%>" name="id"> <INPUT type="hidden"
			value="<%=""%>" name="rename"> <INPUT type="hidden"
			value="<%=group_id%>" name="group_id">
	</form>
</body>
</html>